/**
 * Created by hezhijie on 2016/11/22.
 */
var statisticModule = angular.module("statistic", []);
statisticModule.controller("statisticCtrl", ['$scope',
    function ($scope) {
        $scope.init = function () {
            $scope.queryParams={
                logType: 2,
                orderBy:"user_rate"
            }
            var date=new Date();
            $scope.queryParams.queryPeriod=(date.getFullYear()+"-"+(date.getMonth()<9?"0":"")+(date.getMonth()+1))
            setTimeout(function(){
                $("#queryPeriod").datetimepicker({
                    language:"zh-CN",
                    autoclose:true,
                    todayHighlight:true,
                    minView:"year",
                    startView:"year",
                    format:"yyyy-mm"
                })
            },20)
            $scope.queryStatistic();
        }
        $scope.queryStatistic = function () {
            $post(basePath + "/rest/detect/statistic", $scope.queryParams, function (result) {
                $scope.statisticRecords = result.resultValue;
                $scope.$apply();
                $scope.showChart(result.resultValue)
            })
        }

        $scope.orderByChange=function(){
            $scope.showChart($scope.statisticRecords)
        }

        $scope.showChart=function(records) {
            var labels = [];
            var datas = [];
            records.sort(function(a,b){
                if(b[$scope.queryParams.orderBy]==a[$scope.queryParams.orderBy]){
                    return b["person_count"]-a["person_count"];
                }else{
                    return b[$scope.queryParams.orderBy]-a[$scope.queryParams.orderBy];
                }
            })
            for (var i = 0; i < records.length; i++) {
                labels.push(records[i].dept_name);
                datas.push(records[i][$scope.queryParams.orderBy])
            }

            var statisticChart = echarts.init(document.getElementById("statisticChart"));
            var legend=($scope.queryParams.queryPeriod?$scope.queryParams.queryPeriod:"")+"各个部门/中心涉密检测"+($scope.queryParams.orderBy=="user_rate"?"上报人数":"上报设备")+"比例";
            var option = {
                tooltip: {
                    trigger: "item"
                },
                grid:{
                    y2:130
                },
                legend: {
                    data: [legend]
                },
                xAxis: {
                    name:"部门",
                    data: labels,
                    axisLabel:{
                        inside:true,
                        interval:0,
                        rotate:60,
                        textStyle:{
                            fontWeight:"bold"
                        }
                    }
                },
                yAxis: {
                    name:"上报比例（%）"
                },
                series: [{
                    name: "上报比例",
                    type: "bar",
                    data: datas
                }]
            }
            statisticChart.setOption(option)
        }

        $scope.init();
    }
]);
